<template>
  <div class="container">
    <el-dialog :title="form.id ?   '修改面板' : '新增面板'" :visible="addShow" width="500px" @close="cancel">
      <el-form :model="form" :rules="rules" ref="fromRef">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select v-model="form.subjectID" placeholder="请选择"  style="width:280px">
            <el-option v-for="item in list" :key="item.id" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" label-width="80px" prop="directoryName">
          <el-input v-model="form.directoryName" placeholder="请输入目录名称" style="width:280px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple, add, detail } from '@/api/hmmm/directorys.js'
export default {
  props: ['addShow'],
  data () {
    return {
      form: {
        directoryName: '',
        subjectID: ''
      },
      rules: {
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }],
        subjectID: [{ required: true, message: '请选择所属学科', trigger: 'change' }]
      },
      list: []
    }
  },
  created () {
    // 获取简单列表数据
    this.geySubData()
  },
  methods: {
    // 回填数据
    async getDetail (id) {
      const { data: res } = await detail(id)
      this.form = res
    },
    // 点击确定后 把数据添加到页面中,并且刷新数据列表
    async submit () {
      this.$refs.fromRef.validate(async valid => {
        // 判断表单是否验证通过
        if (!valid) return this.$message.error('验证不通过')
        await add(this.form)
        this.$message.success('新增数据成功')
        // 清空表单中的数据
        this.form = {
          subjectID: '',
          directoryName: ''
        }
        // 关闭面板
        this.$emit('update:addShow', false)
        // 刷新列表数据
        this.$emit('updateDate')
      })
    },
    // 获取学科选项数据
    async geySubData () {
      const { data: res } = await simple()
      this.list = res
    },
    // 关闭面板
    cancel () {
      // 清空表单中的数据
      this.form = {
        name: '',
        region: ''
      }
      // 重置表单
      this.$refs.fromRef.resetFields()
      this.$emit('update:addShow', false)
    }
  }
}
</script>

<style scoped lang="less"></style>
